<?php
class DepartmentController extends Zend_Controller_Action{

    	function init()
	{
		$this->registry = Zend_Registry::getInstance();
		$this->view = $this->registry['view'];
		$this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->template = "department";
		$this->view->conf = new Zend_Config(require './application/configs/config.php');
	}

	public function indexAction(){

	}

        public function addAction(){
            $filter=new Zend_Filter_StripTags();
            $department = new Department();
            if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
                $name=$this->_request->getPost('name');
                $summary=$this->_request->getPost('summary');
                $data = array(
                    'name'=>$name,
                    'summary'=>$summary
                );
                $id = $department->insert($data);
                if($id){
                   echo "<script>window.location='".$this->_request->getBaseUrl()."/department/list';</script>";
                }else{
                     
                }
            }
			echo $this->view->render('department/add.html');
        }

        public function editAction(){
            $filter=new Zend_Filter_StripTags();
            $department = new Department();
            $id=$filter->filter(($this->_request->getParam('id')));
            if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
                $name=$this->_request->getPost('name');
                $summary=$this->_request->getPost('summary');
                $data = array(
                    'name'=>$name,
                    'summary'=>$summary
                );
               $query = $department->updatedepartmentByid($data, $id);
                if($query){
                    echo "<script>window.location='".$this->_request->getBaseUrl()."/department/list';</script>";
                }else{
                     echo "<script>alert('failure!
')</script>";
                }
                
            }else{
                $this->view->department = $department->getdeparmentByid($id);
            }
			echo $this->view->render('department/edit.html');
        }

        public function listAction(){
            $filter=new Zend_Filter_StripTags();
            $department = new Department();
            $page=$filter->filter(($this->_request->getParam('page')));
            if($page==""){
                $page="1";
            }
            $count = $department->getAlldepartments('','');

    //总条数
            $total = count($count);
    //总页数
            $perpage = "10";
            $pages = ceil($total/$perpage);
            $start = ($page-1)*$perpage;
            $end = $perpage;

            $this->view->departments =$department->getAlldepartments($page, $perpage);

            $this->view->page = $page;
            $this->view->pages = $pages;
            $this->view->total = $total;
			echo $this->view->render('department/list.html');
        }

        public  function deleteAction(){
            $filter=new Zend_Filter_StripTags();
            $id=$filter->filter(($this->_request->getParam('id')));
            $department = new Department();
            $query = $department->deldepartmentByid($id);
            if($query){
                    echo "<script>window.location='".$this->_request->getBaseUrl()."/department/list';</script>";
                }else{
                     
                }
        }

}
?>
